package com.njust.controller;

import com.njust.service.*;
import com.njust.utils.DateUtil;
import com.njust.utils.TcpUtil;
import com.njust.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 首页 实时过车显示
 * Created by zhujiawei on 2017/12/28.
 */
@RestController
@Slf4j
@RequestMapping("/")
@Api("首页")
public class RealDisplayController {

    @Autowired
    private TrainService trainService;
    @Autowired
    private MotorService motorService;
    @Autowired
    private GearService gearService;

    /**
     *每次查询最新的数据
     * @return
     */
    @GetMapping("index/last")
    @ApiOperation("每次查询最新的数据")
    public ResultVO indexlast()
            throws ParseException {
        //String test="20171016000000";
        //查询实时信息,当前时间
        Date date = new Date();

        //显示类型20180201000000
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd000000");
        String nowdate = simpleDateFormat.format(date);
        //改了的,注释的为原来的
        ResultVO resultVO = trainService.realShowlast(nowdate);
        return resultVO;
    }

    /**
     * 实时返回所有的数据 以map的作为返回的数据结构
     * @return
     * @throws ParseException
     */
    @GetMapping("index/all")
    @ApiOperation("每次查询最新的数据")
    public ResultVO indexall()
            throws ParseException {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd000000");
        String nowdate = simpleDateFormat.format(date);
        ResultVO resultVO = trainService.realShowall(nowdate);
        return resultVO;
    }

    @GetMapping("/warn/{trainOnlyid}")
    @ApiOperation(value="报警弹出该车的所有故障")
    public ResultVO warn(@PathVariable(name = "trainOnlyid")Long trainOnlyid) throws IOException {
        ResultVO resultVO = trainService.warn(trainOnlyid);
        return resultVO;
    }

    /**
     * 三色灯状态
     * @param warnRank
     * @return
     * @throws IOException
     */
    @GetMapping("/deviceState/{warnRank}")
    @ApiOperation(value="三色灯控制")
    public ResultVO deviceState(@PathVariable(name = "warnRank")Integer warnRank) throws IOException {
        ResultVO resultVO = trainService.deviceState(warnRank);
        return resultVO;
    }

    /**
     * 返回 列车综合数据
     * @param trainOnlyid
     * @return
     */
    @GetMapping("/{trainOnlyid}/traindetail")
    @ApiOperation(value="查看列车综合数据")
    public ResultVO trainDetail(@PathVariable(name = "trainOnlyid")Long trainOnlyid){
        return trainService.findTrainInfoByTrainOnlyid(trainOnlyid);
    }

    /**
     * 返回具体电机综合数据
     * @param trainOnlyid
     * @return
     */
    @GetMapping("/{trainOnlyid}/{trainDirection}/motordetail")
    @ApiOperation(value="电机综合数据")
    public ResultVO motorDetail(@PathVariable(name = "trainOnlyid")Long trainOnlyid,
                                @PathVariable("trainDirection")Integer trainDirection){
        return motorService.findByTrainOnlyid(trainOnlyid,trainDirection);
    }

    /**
     * 前端 列车记录编号和电机号 返回具体的数据
     * @param trainOnlyid
     * @param motorNum
     * @return
     */
    @GetMapping("/{trainOnlyid}/{trainDirection}/{motorNum}/geardetail")
    @ApiOperation(value="齿综合数据")
    public ResultVO gearDetail(@PathVariable(name = "trainOnlyid")Long trainOnlyid,
                               @PathVariable(name = "motorNum")Integer motorNum,
                               @PathVariable("trainDirection")Integer trainDirection){
        return gearService.findByTrainOnlyidAndMotorId(trainOnlyid,motorNum,trainDirection);
    }

    /**
     * 返回左右激光原始数据
     * @param trainOnlyid 列车记录编号
     * @param motorNum 电机编号（1-8）
     * @return
     */
    @GetMapping("/{trainOnlyid}/{trainDirection}/{motorNum}/laser")
    @ApiOperation(value="激光原始数据")
    public ResultVO laserData(@PathVariable(name = "trainOnlyid")Long trainOnlyid,
                              @PathVariable(name = "motorNum") Integer motorNum,
                              @PathVariable("trainDirection")Integer trainDirection){
        return motorService.findByTrainOnlyidAndMotorNum(trainOnlyid,motorNum,trainDirection);
    }

    /**
     * 运营结束后 显示当日气隙最小值
     * @return
     * @throws ParseException
     */
    @GetMapping("today/qiximin/{trainDate}")
    @ApiOperation(value="显示当日气隙最小值")
    public ResultVO QixiMinToday(@PathVariable String trainDate) throws ParseException {
        Date receivetime = DateUtil.String2Date(trainDate);
        long time1 = receivetime.getTime()+12600000;//从三点半开始
        Date firsttime = new Date(time1);
        long time=time1+86400000;
        Date lasttime=new Date(time);
        ResultVO resultVO = trainService.todayLast10(firsttime, lasttime);
        return resultVO;
    }

    /**
     * 新添加一个整点报时并使三色灯亮再恢复绿色主要是为了测试硬件是否正常
     */
    @GetMapping("nowtime")
    public ResultVO clockAndThreeColorlight() throws IOException {
        Date nowDate = new Date();
        String now  = DateUtil.Date2String(nowDate);
        String substring = " ";
        if(now.length()>=14){
            substring = now.substring(10, 14);
        }
       // System.out.println(substring);
        //求出时间的时分秒需要对三色灯进行操作
        if(substring.equals("0000")){
            //表明当前时间为整点需要对三色灯写入数据流操作
            TcpUtil.action(new Integer(2));//延迟两秒，是否可以
           log.info("整点报时");
            TcpUtil.action(new Integer(0));
        }
        return ResultVO.createBySuccessMessage("当前时间为"+ now);
    }





}
